package org.budo.mybatis.dao.statement.builder.result;

import java.util.ArrayList;
import java.util.List;

/**
 * @author li
 */
public class ResultMap {
    private List<Object> properties = new ArrayList<Object>();

    private Class<?> entityType;

    private String id;

    public ResultMap() {}

    public ResultMap(Class<?> entityType, String id) {
        this.entityType = entityType;
        this.id = id;
    }

    public void addProperty(Object property) {
        properties.add(property);
    }

    public String toXml() {
        String xml = "    <resultMap  id=\"" + this.id + "\" type=\"" + this.entityType.getName() + "\">\n";
        for (Object property : properties) {
            if (property instanceof ResultId) {
                xml += ((ResultId) property).toXml() + "\n";
            }
        }
        for (Object property : properties) {
            if (property instanceof ResultProperty) {
                xml += ((ResultProperty) property).toXml() + "\n";
            }
        }
        for (Object property : properties) {
            if (property instanceof ResultAssociation) {
                xml += ((ResultAssociation) property).toXml() + "\n";
            }
        }
        xml += "    </resultMap>";
        return xml;
    }
}